package edu.ycu.aladdin.web.aspect;

import edu.ycu.aladdin.web.common.RestResponse;
import edu.ycu.aladdin.web.exception.AladdinWebException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author <a href="mailto:wangruihuano@gmail.com">王瑞环</a>
 * @since 1.0
 */
@Slf4j
@ControllerAdvice
public class Advice {

    @ResponseBody
    @ExceptionHandler(value = AladdinWebException.class)
    public RestResponse handleUserException(AladdinWebException ex){
        log.warn("server exception",ex);
        return new RestResponse(RestResponse.Type.SERVICE_EXCEPTION,ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public RestResponse handleServerException(Exception ex){
        log.error("server exception",ex);
        return new RestResponse(RestResponse.Type.SERVER_ERROR,ex.getMessage());
    }

}
